namespace Test.ConsoleProgram.Algorithm.Solution
{
    [TestDescription("算法: 0260. 只出现一次的数字(找出现一次的两个数)")]
    public class No0260_SingleNumber3 : AbsBaseTestItem
    {
        public override void OnTest()
        {
            Assert.TestExe(SingleNumber,
                new int[] { 1, 2, 1, 3, 2, 5 },
                new int[] { 3, 5 },
                new int[] { 5, 3 });
        }

        public int[] SingleNumber(int[] nums)
        {
            int value = 0, n = nums.Length;
            for (int i = 0; i < n; i++)
                value ^= nums[i];
            int diff = value & (-value);
            int x = 0;
            for (int i = 0; i < n; i++)
                if ((nums[i] & diff) != 0)
                    x ^= nums[i];
            return new int[] { x, value ^ x };
        }
    }
}
